package com.two.screen.water.balance.utils
import android.content.Context

/**
 * 创建者     Noel
 * 创建时间   2018/1/8 14:30
 * 描述
 */
class ColorUtil {

    companion object {
        // 合成新的颜色值
        fun getNewColorByStartEndColor(context: Context, fraction: Float, startValue: Int, endValue: Int): Int {
            return evaluate(fraction, context.resources.getColor(startValue), context.resources.getColor(endValue))
        }

        /**
         * 合成新的颜色值
         * @param fraction 颜色取值的级别 (0.0f ~ 1.0f)
         * @param startValue 开始显示的颜色
         * @param endValue 结束显示的颜色
         * @return 返回生成新的颜色值
         */
        fun evaluate(fraction: Float, startValue: Int, endValue: Int): Int {
            val startA = startValue shr 24 and 0xff
            val startR = startValue shr 16 and 0xff
            val startG = startValue shr 8 and 0xff
            val startB = startValue and 0xff

            val endA = endValue shr 24 and 0xff
            val endR = endValue shr 16 and 0xff
            val endG = endValue shr 8 and 0xff
            val endB = endValue and 0xff

            return startA + (fraction * (endA - startA)).toInt() shl 24 or
                    (startR + (fraction * (endR - startR)).toInt() shl 16) or
                    (startG + (fraction * (endG - startG)).toInt() shl 8) or
                    startB + (fraction * (endB - startB)).toInt()
        }

    }

}
